https://www.linkedin.com/learning/learning-java-11/
^-- The target audience of this tutorial is geared towards who are genuinely new to programming. Specifically, those who haven't programmed before and their first programming language is Java.
Is this good for those who has prior programming experience yet doesn't know Java?
(SORT OF), thing is she explains it as if you don't know anything about string, data types, etc... etc...
https://www.linkedin.com/learning/advanced-java-programming-2/learn-advanced-java-programming
^-- Okay this one is genuinely advance G.
Okay this is so advance compared to previous one.
2/19/2024
Why learn about object-oriented programming in Java (linkedin.com)
^--- Okay, this one is perfect for me.
When you use the javac
command, which type of file gets created by the compiler?
javac
command creates bytecode that allows a program to run inside a JRE.A string is a reference data type; it is actually a collection of char data types in sequential order.
import java.util.Scanner;
public class InputExample {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// String
System.out.print("Enter your name: ");
String name = scanner.nextLine();
// Number
System.out.print("Enter your number: ");
Int number = scanner.nextInt();
// Display a greeting with the user's name
System.out.println("Hello, " + name + "!");
// Close the Scanner object to release resources
scanner.close();
}
}
public class InputExample {
public static void main(String[] args) {
}
}
The keyword public
indicates that this class is accessible from other classes
The class name (InputExample
in this case) should match the file name (e.g., InputExample.java
)
public static void main(String[] args) {
:
Let's break down the parts of this method signature:
public
: This keyword specifies that the main
method can be called from outside of the class.static
: This keyword indicates that the main
method belongs to the class itself rather than to instances of the class. This allows the Java runtime to call the main
method without creating an object of the class.void
: This specifies that the main
method does not return any value.main
: This is the name of the method. It must be written exactly as main
.(String[] args)
: This is the parameter list for the main
method. It accepts an array of strings as arguments. These arguments can be passed to the program when it is executed from the command line. For example, if you run java InputExample arg1 arg2
, then args
will contain ["arg1", "arg2"]
.static
, it means that the method is associated with the class itself, not with instances (objects) of the class. This means you can call the method directly on the class without needing to create an instance of the class.main
method is typically the entry point of a Java program and it needs to be executed before any objects of the class are created, it must be declared as static
. This allows the Java runtime system to call the main
method without creating an instance of the class containing the main
method.We add the keyword static if the function does not use the attributes of a class, but still relates to the overall theme or idea of the class
An instance is an object created from a class blueprint.
How to create instances?
A constructor always returns an instance of the class it's in (that is, a constructor in the Triangle class returns a triangle).
How to use constructor?
We call it like any other function
The only difference is that because we are creating a new Triangle
instance,
we have to use the new
keyword
ClassName instanceName = new ConstructorName();
public class Triangle {
double base;
double height;
// Constructor Declaration
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// Method below
public double findArea() {
this.base = this.height / 2;
}
}
What is true about a constructor function?
It always returns an instance of the class that it is in.
When is an attribute accessible in a class?
If numOfSides
is a static class integer variable of the Triangle
class, then how would you correctly access this variable inside the main
function?
System.out.println(mytriangle.numOfSides);
Incorrect
An object instance is not needed for a static class variable.
System.out.println(Triangle.numOfSides);
For static variables, you can type the class name followed by a period (dot) to access it.
Instance methods are also referred to as non-static methods since you need an instance to use them
Class methods are referred to as static methods because you do not need an instance to use them
pow()
, charAt()
, println()
Generics are a way to tell a compiler what type of object a collection can contain.